import { getUserInfo, login } from '../../api/index';

export const _toggleLoading = (payload) => {
  return {
    type: 'TOGGLE_LOADING',
    payload,
  };
};

export const _getUserInfoData = (payload) => {
  return {
    type: 'GET_USER_INFO',
    payload,
  };
};

export const _postUserInfoData = (payload) => {
  return {
    type: 'POST_USER_LOGIN',
    payload,
  };
};

export const _postUserLogoutData = () => {
  return {
    type: 'POST_USER_LOGOUT',
  };
};

export const toggleShowLoginModal = (payload) => {
  return {
    type: 'TOGGLE_SHOWLOGINMODAL',
    payload,
  };
};

export function postUserLogin(data) {
  return async (dispatch) => {
    try {
      await dispatch(_toggleLoading(true));
      const result = await login(data);

      await dispatch(_postUserInfoData(result));
      await dispatch(_toggleLoading(false));
      await dispatch(toggleShowLoginModal(false));
    } catch (error) {
      console.log(error);
    }
  };
}

export function postUserLogout() {
  return async (dispatch) => {
    try {
      await dispatch(_postUserLogoutData());
    } catch (error) {
      console.log(error);
    }
  };
}
